<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2021/9/6
 * Time: 13:59
 */
namespace app\admin\controller;

use app\middleware\AdminCheck;
use think\facade\Db;

class Chapter {

    protected $middleware = [
        AdminCheck::class => ['except' 	=> [''] ]
    ];


    /**
     *   @SWG\Post(
     *     path="/admin/chapter/cityList",
     *     tags={"后台考试相关"},
     *     summary="城市列表",
     *     consumes={"application/json"},
     *     produces={"application/json"},
     *     @SWG\Parameter(
     *      in="query",
     *      required=true,
     *      name="",
     *      type="string",
     *      description="不需要参数"),
     *     @SWG\Response(response="200",description="ok")
     *  )
     */
    /**
     * 城市列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function cityList(){
        $region = Db::name('region')
            ->select()->toArray();
        return success('城市列表',$region);
    }



    /**
     *   @SWG\Post(
     *     path="/admin/chapter/add",
     *     tags={"后台考试相关"},
     *     summary="添加章节",
     *     consumes={"application/json"},
     *     produces={"application/json"},
     *     @SWG\Parameter(
     *      in="query",
     *      required=true,
     *      name="cid_,chapter_,city_,cid_,chaper_,city_,total_,judge_num,single_sel_num,muti_sel_num",
     *      type="string",
     *      description="cid_(第几张数字),chapter_（章节名称）,city_（城市）,total_(总题目数量),judge_num（判断题数量）,single_sel_num（单选题数量）,muti_sel_num（多选题数量）"),
     *     @SWG\Response(response="200",description="ok")
     *  )
     */
    /**
     * 添加章节
     * @return \think\response\Json
     */
    public function add(){
        validateParam('cid_,chaper_,city_,total_,judge_num,single_sel_num,muti_sel_num','cid_,chaper_,city_,cid_,chaper_,city_,total_,judge_num,single_sel_num,muti_sel_num');
        $data['cid_']=input('cid_');
        $data['chaper_']=input('chaper_');
        $data['total_']=input('total_');
        $data['judge_num']=input('judge_num');
        $data['single_sel_num']=input('single_sel_num');
        $data['muti_sel_num']=input('muti_sel_num');
        $data['pid_']=0;
        $data['city_']=input('city_');
        $data['update_at'] = currentTime();
        $data['create_at'] = currentTime();
        Db::name('chapter')->insert($data);
        return success('添加成功');
    }




    /**
     *   @SWG\Post(
     *     path="/admin/chapter/getList",
     *     tags={"后台考试相关"},
     *     summary="章节列表",
     *     consumes={"application/json"},
     *     produces={"application/json"},
     *     @SWG\Parameter(
     *      in="query",
     *      required=true,
     *      name="pageNo,pageSize",
     *      type="string",
     *      description="pageNo(第几页),pageSize（一页多少条数据）,city_（城市）"),
     *     @SWG\Response(response="200",description="ok")
     *  )
     */
    /**
     * 章节列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getList(){
        validateParam('city_,pageNo,pageSize','city_,pageNo,pageSize');
        $param=input();
        $where[]=['city_','=',$param['city_']];
        //获取总数
        $total=Db::name('chapter')
            ->where($where)
            ->count();
        $offset=makeOffset($param['pageNo'],$param['pageSize']);

        $list=Db::name('chapter')
            ->where($where)
            ->limit($offset,$param['pageSize'])
            ->select();
        return success('章节列表',['total'=>$total,'list'=>$list]);
    }



    /**
     *   @SWG\Post(
     *     path="/admin/chapter/edit",
     *     tags={"后台考试相关"},
     *     summary="编辑",
     *     consumes={"application/json"},
     *     produces={"application/json"},
     *     @SWG\Parameter(
     *      in="query",
     *      required=true,
     *      name="id_,cid_,chapter_,city_,cid_,chaper_,city_,total_,judge_num,single_sel_num,muti_sel_num",
     *      type="string",
     *      description="id_,cid_(第几张数字),chapter_（章节名称）,city_（城市）,total_(总题目数量),judge_num（判断题数量）,single_sel_num（单选题数量）,muti_sel_num（多选题数量）"),
     *     @SWG\Response(response="200",description="ok")
     *  )
     */
    /**
     * 编辑章节
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function edit(){
        validateParam('id_,cid_,chaper_,city_,total_,judge_num,single_sel_num,muti_sel_num','id_,cid_,chaper_,city_,cid_,chaper_,city_,total_,judge_num,single_sel_num,muti_sel_num');
        $data['cid_']=input('cid_');
        $data['chaper_']=input('chaper_');
        $data['total_']=input('total_');
        $data['judge_num']=input('judge_num');
        $data['single_sel_num']=input('single_sel_num');
        $data['muti_sel_num']=input('muti_sel_num');
        $data['pid_']=0;
        $data['city_']=input('city_');
        $data['update_at'] = currentTime();
        Db::name('chapter')
            ->where('id_',input('id_'))
            ->update($data);
        return success('编辑成功');
    }




    /**
     *   @SWG\Post(
     *     path="/admin/chapter/delete",
     *     tags={"后台考试相关"},
     *     summary="删除章节",
     *     consumes={"application/json"},
     *     produces={"application/json"},
     *     @SWG\Parameter(
     *      in="query",
     *      required=false,
     *      name="id",
     *      type="string",
     *      description="id"),
     *     @SWG\Response(response="200",description="ok")
     *  )
     */
    /**
     * 删除章节
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function delete(){
        validateParam('id_','id_');
        //首先判断章节是否存在题库
        $question = Db::name('question')
            ->where('chapter_',input('id_'))
            ->find();
        if(!empty($question)){
            return error('章节下存在题目，不可删除');
        }
        Db::name('chapter')
            ->where('id_','=',input('id_'))
            ->delete();
        return success('删除成功');
    }



}